﻿package com.vicentediaz.view.general
{
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.vicentediaz.data.*;
    import flash.display.*;
    import flash.events.*;

    public class ExplodeButton extends Sprite
    {
        private var rollovered:Boolean;
        private var initWidth:uint;
        private var initHeight:uint;
        private const ANIMATE_TIME:Number = 0.2;

        public function ExplodeButton()
        {
            return;
        }// end function

        private function randomizePixels(param1:MouseEvent) : void
        {
            var _loc_3:Bitmap;
            if (this.rollovered)
            {
                return;
            }// end if
            this.rollovered = true;
            var _loc_2:* = Sprite(param1.currentTarget);
            var _loc_6:uint;
            while (_loc_6++ < _loc_2.numChildren)
            {
                // label
                _loc_3 = _loc_2.getChildAt(_loc_6) as Bitmap;
                TweenMax.to(_loc_3, this.ANIMATE_TIME, {x:_loc_3.x - 5 + int(Math.random() * 10), y:_loc_3.y - 5 + int(Math.random() * 10), alpha:Math.random(), ease:Quad.easeOut});
            }// end while
            Sounds.getInstance().playById("GLASS", 0.1);
            return;
        }// end function

        public function createFrom(param1:DisplayObject) : Sprite
        {
            var _loc_4:Bitmap;
            var _loc_5:BitmapData;
            var _loc_7:uint;
            var _loc_2:* = new Sprite();
            var _loc_3:* = new BitmapData(param1.width, param1.height, true, 16711680);
            _loc_3.draw(param1);
            this.initWidth = param1.width;
            this.initHeight = param1.height;
            var _loc_6:uint;
            while (_loc_6++ <= param1.height)
            {
                // label
                _loc_7 = 0;
                while (_loc_7++ <= param1.width)
                {
                    // label
                    _loc_5 = new BitmapData(1, 1, true, 16711680);
                    _loc_5.setPixel32(0, 0, _loc_3.getPixel32(_loc_7, _loc_6));
                    _loc_4 = new Bitmap(_loc_5, "auto", false);
                    _loc_4.x = _loc_7;
                    _loc_4.y = _loc_6;
                    _loc_2.addChild(_loc_4);
                }// end while
            }// end while
            _loc_2.graphics.beginFill(0, 0);
            _loc_2.graphics.drawRect(-1, -1, this.initWidth + 1, this.initHeight + 1);
            _loc_2.graphics.endFill();
            _loc_2.mouseChildren = false;
            _loc_2.addEventListener(MouseEvent.ROLL_OVER, this.randomizePixels, false, 0, true);
            _loc_2.addEventListener(MouseEvent.ROLL_OUT, this.backToOriginal, false, 0, true);
            return _loc_2;
        }// end function

        private function backToOriginal(param1:MouseEvent) : void
        {
            var _loc_3:Bitmap;
            this.rollovered = false;
            var _loc_2:* = Sprite(param1.currentTarget);
            var _loc_4:* = this.initWidth + 1;
            var _loc_5:uint;
            while (_loc_5++ < _loc_2.numChildren)
            {
                // label
                _loc_3 = _loc_2.getChildAt(_loc_5) as Bitmap;
                TweenMax.to(_loc_3, this.ANIMATE_TIME, {x:_loc_5 % _loc_4, y:int(_loc_5 / _loc_4), alpha:1});
            }// end while
            return;
        }// end function

    }
}
